package com.ocrgroup.sample.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.etop.bank.model.OcrKeyDatas;
import com.etop.bank.utils.StreamUtil;
import com.example.bank_ocr.R;
import com.ocrgroup.sample.adapter.OcrResultImgAdapter;
import com.ocrgroup.sample.bean.ResultListInfo;
import com.ocrgroup.sample.view.GalleryTransformer;
import com.ocrgroup.sample.view.HackyViewPager;

import java.util.ArrayList;
import java.util.List;


public class OcrResultActivity extends AppCompatActivity {

    private TextView imgNums;
    private int recogCode;// 识别码
    private int recogType;
    private String headPath;  // 头像地址
    private String cropPath;  // 裁切图地址
    private String imagePath;  // 原图地址
    private List<ResultListInfo> listResult;  // 识别结果集合

    private ArrayList<Bitmap> bitmaps = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ocr_result);
        // 初始化数据
        initData();
        // 初始化view
        initView();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        // 获取识别结果
        listResult = (List<ResultListInfo>) getIntent().getSerializableExtra(OcrKeyDatas.RECOG_RESULT_LIST);
        recogCode = getIntent().getIntExtra(OcrKeyDatas.RECOG_CODE, -1);
        // 识别类型
        recogType = getIntent().getIntExtra(OcrKeyDatas.RECOG_TYPE, 0);
        headPath = getIntent().getStringExtra(OcrKeyDatas.HEAD_PATH);
        imagePath = getIntent().getStringExtra(OcrKeyDatas.IMAGE_PATH);
        cropPath = getIntent().getStringExtra(OcrKeyDatas.CROP_PATH);
        // 标题
        String title = getIntent().getStringExtra(OcrKeyDatas.TITLE);
        setTitle(title + "识别结果");

        int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        // 获取压缩图
        Bitmap headBitmap = StreamUtil.getSmallBitmap(headPath, screenWidth, screenHeight);
        Bitmap cropBitmap = StreamUtil.getSmallBitmap(cropPath, screenWidth, screenHeight);
        Bitmap imageBitmap = StreamUtil.getSmallBitmap(imagePath, screenWidth, screenHeight);
        if (cropBitmap != null)
            bitmaps.add(cropBitmap);
        if (headBitmap != null)
            bitmaps.add(headBitmap);
        if (imageBitmap != null)
            bitmaps.add(imageBitmap);
    }

    /**
     * 初始化view
     */
    private void initView() {
        HackyViewPager ocr_img = findViewById(R.id.ocr_img);
        RecyclerView ocr_list = findViewById(R.id.ocr_list);
        imgNums = findViewById(R.id.ocr_img_count);
        // 数量
        imgNums.setText(String.format("%d/%d", 1, bitmaps.size()));

        ocr_img.setOffscreenPageLimit(3);
        ocr_img.setPageMargin(30);
        ocr_img.setAdapter(new OcrResultImgAdapter(this, bitmaps));
        ocr_img.setPageTransformer(false, new GalleryTransformer());
        // 图片滑动监听
        ocr_img.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            //切换（滑动）到界面的时候调用方法
            @Override
            public void onPageSelected(int position) {
                // 数量
                imgNums.setText(String.format("%d/%d", position + 1, bitmaps.size()));
            }


            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
//		//事件分发，处理页面滑动问题
//		mRelativeLayout.setOnTouchListener(new View.OnTouchListener() {
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				return ocr_img.dispatchTouchEvent(event);
//			}
//		});

        // 列表
        ocr_list.setLayoutManager(new LinearLayoutManager(this));
        // 设置间距
//		ocr_list.setPadding(40, 20, 40, 20);
        // 设置适配器
        OcrListAdapter listAdapter = new OcrListAdapter();
        ocr_list.setAdapter(listAdapter);
    }

    /**
     * 文件列表适配器
     */
    static class RecyleViewHolder extends RecyclerView.ViewHolder {
        TextView ocrKey;
        TextView ocrValue;
        LinearLayout ocrItem;

        RecyleViewHolder(View itemView) {
            super(itemView);
            //初始化控件
            ocrKey = itemView.findViewById(R.id.ocr_item_key);
            ocrValue = itemView.findViewById(R.id.ocr_item_value);
            ocrItem = itemView.findViewById(R.id.ocr_item);
        }
    }

    private class OcrListAdapter extends RecyclerView.Adapter<RecyleViewHolder> {

        @NonNull
        @Override
        public RecyleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            //解决宽度不能铺满
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_result, viewGroup, false);
            RecyleViewHolder recyleViewHolder = new RecyleViewHolder(view);
            // 禁止复用
            recyleViewHolder.setIsRecyclable(false);
            return recyleViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyleViewHolder viewHolder, final int position) {
            if (position % 2 == 0) {
                viewHolder.ocrItem.setBackgroundResource(R.drawable.item_result_shape);
            } else {
                viewHolder.ocrItem.setBackgroundResource(R.drawable.item_result_white_shape);
            }
            ResultListInfo info = listResult.get(position);
            if (recogCode == 0) {
                viewHolder.ocrValue.setText(info.getValue());
                viewHolder.ocrKey.setText(info.getKey());
            } else {
                viewHolder.ocrKey.setText("识别失败");
                viewHolder.ocrValue.setText(info.getValue());
            }
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return listResult.size();
        }

    }

}
